<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/14
 * Time: 14:31
 */

namespace app\api\controller;

use app\api\model\Area AS AreaModel;
use app\api\model\Order AS OrderModel;
use app\api\model\OrderInfo AS OrderInfoModel;
use app\api\model\Address AS AddressModel;
use app\api\model\OrderCg AS OrderCgModel;
use app\api\model\OrderInfoCg AS OrderInfoCgModel;
use app\api\validate\IdMustPositiveInteger;
use app\api\validate\UserIdCheck;
use think\Db;

class Order extends Base
{
    /**
     * 新增/更新订单草稿
     * user_id      用户ID
     * username     报修人
     * phone        报修人手机号
     * company      公司
     * sheng        省ID
     * shi          市ID
     * qu           区ID
     * address      详细地址
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderCg(){
        $user_id = input('user_id');
        $order_username = input('username');
        $order_phone = input('phone');
        $order_company = input('company');
        $order_sheng = input('sheng');
        $order_shi = input('shi');
        $order_qu = input('qu');
        $order_address = input('address');

        $resultUserId = (new UserIdCheck())->goCheck();
        if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

        $ORDERCG = new OrderCgModel();

        $in = $ORDERCG::findOrderCg(['order_user_id' => $user_id], 'order_id');
        $order = [
            'order_username' => $order_username,
            'order_phone' => $order_phone,
            'order_company' => $order_company,
            'order_sheng' => $order_sheng,
            'order_shi' => $order_shi,
            'order_qu' => $order_qu,
            'order_address' => $order_address,
            'order_update_time' => createTime()
        ];

        $AREA = new AreaModel();
        $province = $AREA::findArea(['district_id' => $order_sheng], 'district');
        $city = $AREA::findArea(['district_id' => $order_shi], 'district');
        $qu = $AREA::findArea(['district_id' => $order_qu], 'district');
        $order['order_address_str'] = $province['district'] . $city['district'] . $qu['district'] . $order_address;

        if($in){
            $res = $ORDERCG::update(['order_id' => $in['order_id'], $order]);
            if($res){
                return json(['status' => 200, 'msg' => $this->doSuccess, 'order_id' => $in['order_id']]);
            }else{
                return json(['status' => 0, 'msg' => $this->systemErr, 'order_id' => $in['order_id']]);
            }
        }else{
            $order['order_user_id'] = $user_id;
            $order_id = $ORDERCG::insertOrderCg($order);
            if($order_id){
                return json(['status' => 200, 'msg' => $this->doSuccess, 'order_id' => $order_id]);
            }else{
                return json(['status' => 0, 'msg' => $this->systemErr, 'order_id' => $order_id]);
            }
        }
    }

    /**
     * 新增/更新订单草稿明细
     * id               明细ID
     * oi_xl            系列ID
     * oi_xh            型号ID
     * oi_gz            故障ID
     * oi_xulie         序列号
     * oi_desc          故障描述
     * oi_front_image   前图片
     * oi_side_image    侧图片
     * oi_mp_image      名牌图片
     * oi_other_image   其它图片
     * oi_other_video   其它视频
     * @return \think\response\Json
     */
    public function orderInfoCg(){
        $oi_id = input('id') ? input('id') : 0;
        $order_id = input('order_id');
        $oi_xl = input('oi_xl');
        $oi_xh = input('oi_xh');
        $oi_gz = input('oi_gz');
        $oi_xulie = input('oi_xulie');
        $oi_desc = input('oi_desc');
        $oi_front_image = input('oi_front_image');
        $oi_side_image = input('oi_side_image');
        $oi_mp_image = input('oi_mp_image');
        $oi_other_image = input('oi_other_image');
        $oi_other_video = input('oi_other_video');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $orderInfo = [
            'oi_order_id' => $order_id,
            'oi_xl' => $oi_xl,
            'oi_xh' => $oi_xh,
            'oi_gz' => $oi_gz,
            'oi_xulie' => $oi_xulie,
            'oi_desc' => $oi_desc,
            'oi_front_image' => $oi_front_image,
            'oi_side_image' => $oi_side_image,
            'oi_mp_image' => $oi_mp_image,
            'oi_other_image' => $oi_other_image,
            'oi_other_video' => $oi_other_video,
            'oi_update_time' => createTime()
        ];

        $ORDERINFOCG = new OrderInfoCgModel();
        if($oi_id){
            $res = $ORDERINFOCG::updateOrderInfoCg(['oi_id' => $oi_id], $orderInfo);
            if($res){
                return json(['status' => 200, 'msg' => $this->doSuccess]);
            }else{
                return json(['status' => 0, 'msg' => $this->systemErr]);
            }
        }else{
            $res = $ORDERINFOCG::insertOrderInfoCg($orderInfo);
            if($res){
                return json(['status' => 200, 'msg' => $this->doSuccess, 'oi_id' => $res]);
            }else{
                return json(['status' => 0, 'msg' => $this->systemErr, 'oi_id' => $res]);
            }
        }
    }

    /**
     * 删除订单名称草稿
     * id    订单名称草稿ID
     * @return \think\response\Json
     */
    public function delOrderInfoCg(){
        $oi_id = input('id');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDERINFOCG = new OrderInfoCgModel();
        $res = $ORDERINFOCG::delOrderInfoCg(['oi_id' => $oi_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->doSuccess]);
        }else{
            return json(['status' => 0, 'msg' => $this->systemErr]);
        }
    }

    /**
     * 订单草稿详情
     * user_id     用户ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderDetailCg(){
        $user_id = input('user_id');

        $resultUserId = (new UserIdCheck())->goCheck();
        if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

        $ORDERCG = new OrderCgModel();
        $where['order_user_id'] = $user_id;
        $field = 'order_id, order_username, order_phone, order_company, order_sheng, order_shi, 
        order_qu, order_address, order_address_str, a1.district sheng, a2.district shi, a3.district qu';
        $order = $ORDERCG::findJoinOrderCg($where, $field);

        if($order){
            $ORDERINFOCG = new OrderInfoCgModel();
            $whereInfo['oi_order_id'] = $order['order_id'];
            $fieldInfo = 'x1.device_name xl_name, x2.device_name xh_name, oi_xl, oi_xh, oi_gz, oi_xulie, gz_name, oi_desc, oi_front_image, 
        oi_side_image, oi_mp_image, oi_other_image, oi_other_video';
            $orderInfo = $ORDERINFOCG::selectJoinOrderInfoCg($whereInfo, $fieldInfo);
            foreach ($orderInfo as $k => $v){
                if($v['oi_other_image']){
                    $orderInfo[$k]['oi_other_image'] = explode(',', $v['oi_other_image']);
                }else{
                    $orderInfo[$k]['oi_other_image'] = [];
                }
            }
        }else{
            $orderInfo = [];
        }

        return json(['status' => 200, 'msg' => '', 'order' => $order, 'orderInfo' => $orderInfo]);
    }

    /**
     * 保存草稿
     * user_id      用户ID
     * username     报修人
     * phone        报修人手机号
     * company      公司
     * sheng        省ID
     * shi          市ID
     * qu           区ID
     * address      详细地址
     * device       设备
     *      oi_xl            系列ID
     *      oi_xh            型号ID
     *      oi_gz            故障ID
     *      oi_xulie         序列号
     *      oi_desc          故障描述
     *      oi_front_image   前图片
     *      oi_side_image    侧图片
     *      oi_mp_image      名牌图片
     *      oi_other_image   其它图片
     *      oi_other_video   其它视频
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createOrderCg(){
        $user_id = input('user_id');
        $order_username = input('username');
        $order_phone = input('phone');
        $order_company = input('company');
        $order_sheng = input('sheng');
        $order_shi = input('shi');
        $order_qu = input('qu');
        $order_address = input('address');
        $device = input('device');

        $resultUserId = (new UserIdCheck())->goCheck();
        if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

        if($device){
            $device = json_decode($device, true);
        }

        $insertOrderInfo = [];
        if(is_array($device) && !empty($device)){
            foreach ($device as $k => $v){
                if(!$v['oi_xl']) return json(['status' => 0, 'msg' => '请选择系列']);
                if(!$v['oi_xh']) return json(['status' => 0, 'msg' => '请选择型号']);
                if(!$v['oi_gz']) return json(['status' => 0, 'msg' => '请选择故障']);
                if(!$v['oi_xulie']) return json(['status' => 0, 'msg' => '请输入序列号']);
                $insertOrderInfo[$k] = [
                    'oi_xl' => $v['oi_xl'],
                    'oi_xh' => $v['oi_xh'],
                    'oi_gz' => $v['oi_gz'],
                    'oi_xulie' => $v['oi_xulie'],
                    'oi_desc' => $v['oi_desc'],
                    'oi_front_image' => $v['oi_front_image'],
                    'oi_side_image' => $v['oi_side_image'],
                    'oi_mp_image' => $v['oi_mp_image'],
                    'oi_other_image' => $v['oi_other_image'],
                    'oi_other_video' => $v['oi_other_video']
                ];
            }
        }

        $insertOrder = [
            'order_user_id' => $user_id,
            'order_username' => $order_username,
            'order_phone' => $order_phone,
            'order_company' => $order_company,
            'order_sheng' => $order_sheng,
            'order_shi' => $order_shi,
            'order_qu' => $order_qu,
            'order_address' => $order_address,
            'order_update_time' => createTime()
        ];

        $AREA = new AreaModel();
        $province = $AREA::findArea(['district_id' => $order_sheng], 'district');
        $city = $AREA::findArea(['district_id' => $order_shi], 'district');
        $qu = $AREA::findArea(['district_id' => $order_qu], 'district');
        $insertOrder['order_address_str'] = $province['district'] . $city['district'] . $qu['district'] . $order_address;

        Db::startTrans();
        try{
            $ORDERCG = new OrderCgModel();
            $ORDERINFOCG = new OrderInfoCgModel();

            $hasOrderCg = $ORDERCG::findOrderCg(['order_user_id' => $user_id], 'order_id');
            if($hasOrderCg){
                $ORDERCG::delOrderCg(['order_id' => $hasOrderCg['order_id']]);
                $hasOrderInfo = $ORDERINFOCG::findOrderInfoCg(['oi_order_id' => $hasOrderCg['order_id']]);
                if($hasOrderInfo){
                    $ORDERINFOCG::delOrderInfoCg(['oi_order_id' => $hasOrderCg['order_id']]);
                }
            }

            $order_id = $ORDERCG::insertOrderCg($insertOrder);

            if($device){
                foreach ($insertOrderInfo as $ki => $vi){
                    $insertOrderInfo[$ki]['oi_order_id'] = $order_id;
                }
                $ORDERINFOCG::saveOrderInfo($insertOrderInfo);
            }

            Db::commit();
            return json(['status' => 200, 'msg' => '提交成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 提交维修申请
     * user_id      用户ID
     * username     报修人
     * phone        报修人手机号
     * company      公司
     * sheng        省ID
     * shi          市ID
     * qu           区ID
     * address      详细地址
     * device       设备
     *      oi_xl            系列ID
     *      oi_xh            型号ID
     *      oi_gz            故障ID
     *      oi_xulie         序列号
     *      oi_desc          故障描述
     *      oi_front_image   前图片
     *      oi_side_image    侧图片
     *      oi_mp_image      名牌图片
     *      oi_other_image   其它图片
     *      oi_other_video   其它视频
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createOrder(){
         $user_id = input('user_id');
         $order_username = input('username');
         $order_phone = input('phone');
         $order_company = input('company');
         $order_sheng = input('sheng');
         $order_shi = input('shi');
         $order_qu = input('qu');
         $order_address = input('address');
         $device = input('device');
         $resultUserId = (new UserIdCheck())->goCheck();
         if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

         if($device){
             $device = json_decode($device, true);
         }else{
             return json(['status' => 0, 'msg' => '请添加需要报修的设备']);
         }

         $AREA = new AreaModel();

         $insertOrderInfo = [];
         $order_number = 0; // 报修设备总数
         if(is_array($device)){
             foreach ($device as $k => $v){
                 if(!$v['oi_xl']) return json(['status' => 0, 'msg' => '请选择系列']);
                 if(!$v['oi_xh']) return json(['status' => 0, 'msg' => '请选择型号']);
                 if(!$v['oi_gz']) return json(['status' => 0, 'msg' => '请选择故障']);
                 if(!$v['oi_xulie']) return json(['status' => 0, 'msg' => '请输入序列号']);
                 $insertOrderInfo[$k] = [
                     'oi_xl' => $v['oi_xl'],
                     'oi_xh' => $v['oi_xh'],
                     'oi_gz' => $v['oi_gz'],
                     'oi_xulie' => $v['oi_xulie'],
                     'oi_desc' => $v['oi_desc'],
                     'oi_front_image' => $v['oi_front_image'],
                     'oi_side_image' => $v['oi_side_image'],
                     'oi_mp_image' => $v['oi_mp_image'],
                     'oi_other_image' => $v['oi_other_image'],
                     'oi_other_video' => $v['oi_other_video']
                 ];
             }
             $order_number = count($device);
         }else{
             return json(['status' => 0, 'msg' => '数据格式错误']);
         }

        $order_no = date('YmdHis') . rand(0000, 9999);
        $insertOrder = [
            'order_no' => $order_no,
            'order_user_id' => $user_id,
            'order_username' => $order_username,
            'order_phone' => $order_phone,
            'order_company' => $order_company,
            'order_sheng' => $order_sheng,
            'order_shi' => $order_shi,
            'order_qu' => $order_qu,
            'order_address' => $order_address,
            'order_number' => $order_number,
            'order_time' => createTime()
        ];
        $province = $AREA::findArea(['district_id' => $order_sheng], 'district');
        $city = $AREA::findArea(['district_id' => $order_shi], 'district');
        $qu = $AREA::findArea(['district_id' => $order_qu], 'district');
        $insertOrder['order_address_str'] = $province['district'] . $city['district'] . $qu['district'] . $order_address;

         Db::startTrans();
         try{
             // 订单表
             $ORDER = new OrderModel();
             $order_id = $ORDER::insertOrder($insertOrder);

             // 订单明细
             foreach ($insertOrderInfo as $ki => $vi){
                 $insertOrderInfo[$ki]['oi_order_id'] = $order_id;
             }
             $ORDERINFO = new OrderInfoModel();
             $ORDERINFO::saveOrderInfo($insertOrderInfo);

             // 删除草稿
             $ORDERCG = new OrderCgModel();
             $ORDERINFOCG = new OrderInfoCgModel();
             $hasOrderCg = $ORDERCG::findOrderCg(['order_user_id' => $user_id], 'order_id');
             if($hasOrderCg){
                 $ORDERCG::delOrderCg(['order_id' => $hasOrderCg['order_id']]);
                 $hasOrderInfo = $ORDERINFOCG::findOrderInfoCg(['oi_order_id' => $hasOrderCg['order_id']]);
                 if($hasOrderInfo){
                     $ORDERINFOCG::delOrderInfoCg(['oi_order_id' => $hasOrderCg['order_id']]);
                 }
             }

             Db::commit();
             return json(['status' => 200, 'msg' => '提交成功']);
         }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
         }
    }

    /**
     * 我的订单状态
     * user_id      用户ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderStatus(){
        $user_id = input('user_id');

        $resultUserId = (new UserIdCheck())->goCheck();
        if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

        $where['order_user_id'] = $user_id;
        $ORDER = new OrderModel();
        $where['order_status'] = 1;
        $unCheck = $ORDER::countOrder($where); // 待审核
        $where['order_status'] = 4;
        $backIng = $ORDER::countOrder($where); // 返厂中
        $where['order_status'] = 5;
        $xiuIng = $ORDER::countOrder($where); // 维修中
        $where['order_status'] = 6;
        $alreadyBack = $ORDER::countOrder($where); // 已寄回
        $where['order_status'] = 7;
        $alreadyOver = $ORDER::countOrder($where); // 已完成

        return json([
            'status' => 200,
            'msg' => '',
            'unCheck' => $unCheck,
            'backIng' => $backIng,
            'xiuIng' => $xiuIng,
            'alreadyBack' => $alreadyBack,
            'alreadyOver' => $alreadyOver
        ]);
    }

    /**
     * 用户订单列表
     * user_id  用户ID
     * status   状态   0 全部  1 待审核  4 返厂中  5 维修中  6 已寄回  7 已完成
     * page     页码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderList(){
        $user_id = input('user_id');
        $status = input('status') ? input('status') : 0;
        $page = input('page') ? input('page') : 1;

        $resultUserId = (new UserIdCheck())->goCheck();
        if($resultUserId['status'] == 0) return json(['status' => 0, 'msg' => $resultUserId['msg']]);

        $where['order_user_id'] = $user_id;

        $ORDER = new OrderModel();
        $count = $ORDER::countOrder($where);

        if($status != 0){
            if($status == 4){
                $where['order_status'] = ['BETWEEN', [3, 4]];
            }else{
                $where['order_status'] = $status;
            }

        }
        $field = 'order_id, order_no, order_time, order_status,order_jiance_status, order_number';

        $list = $ORDER::selectOrder($where, $field, $page);
        if($list){
            $order_ids = array_column($list, 'order_id');
            $whereInfo['oi_order_id'] = ['IN', $order_ids];
            $fieldInfo = 'oi_order_id, device_name';
            $ORDERINFO = new OrderInfoModel();
            $info = $ORDERINFO::selectJoinXl($whereInfo, $fieldInfo);
            foreach ($list as $k => $v){
                $xl = '';
                foreach ($info as $ki => $vi){
                    if($vi['oi_order_id'] == $v['order_id']){
                        $xl .= $vi['device_name'] . ',';
                    }
                }
                $list[$k]['device_name'] = trim($xl, ',');
            }
            return json(['status' => 200, 'msg' => '', 'data' => $list, 'count' => $count]);
        }else{
            return json(['status' => 200, 'msg' => '', 'data' => [], 'count' => $count]);
        }
    }

    /**
     * 订单详情
     * id      订单ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderDetail(){
        $order_id = input('id');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDER = new OrderModel();
        $where['order_id'] = $order_id;
        $field = 'order_no, order_username, order_phone, order_company, order_address_str, order_remark, 
        order_status, order_refuse_reason, order_company_express_company, order_company_express_code, 
        order_company_name, order_company_phone, order_company_address, order_user_express_company, 
        order_user_express_code, order_user_name, order_user_phone, order_user_address
        , order_company_express_type, order_jiance_status,  order_jiance_remark, order_nj_user2, order_nj_user3';
        $order = $ORDER::findOrder($where, $field);
        $array = $this->arrayEmpty;
        $order['order_nj_user2'] = $order['order_nj_user2'] ? json_decode($order['order_nj_user2'],true) : $array;
        $order['order_nj_user3'] = $order['order_nj_user3'] ? json_decode($order['order_nj_user3'],true) : $array;
        $ORDERINFO = new OrderInfoModel();
        $whereInfo['oi_order_id'] = $order_id;
        $fieldInfo = 'x1.device_name xl_name, x2.device_name xh_name, oi_xulie, gz_name, oi_desc, oi_front_image, 
        oi_side_image, oi_mp_image, oi_other_image, oi_other_video, oi_wx_report_url, oi_wx_report_dis, 
        oi_wx_report_name';
        $orderInfo = $ORDERINFO::selectJoin($whereInfo, $fieldInfo);
        foreach ($orderInfo as $k => $v){
            if($v['oi_other_image']){
                $orderInfo[$k]['oi_other_image'] = explode(',', $v['oi_other_image']);
            }else{
                $orderInfo[$k]['oi_other_image'] = [];
            }
        }

        return json(['status' => 200, 'msg' => '', 'order' => $order, 'orderInfo' => $orderInfo]);
    }

    /**
     * 取消订单
     * id      订单ID
     * @return \think\response\Json
     */
    public function cancelOrder(){
        $order_id = input('id');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDER = new OrderModel();
        $where['order_id'] = $order_id;
        $update = [
            'order_status' => 8,
            'order_cancel_time' => createTime()
        ];
        $res = $ORDER::updateOrder($where, $update);
        if($res){
            return json(['status' => 200, 'msg' => $this->doSuccess]);
        }else{
            return json(['status' => 0, 'msg' => $this->systemErr]);
        }
    }

    /**
     * 用户发货
     * id      订单ID
     * express_company    快递公司
     * express_code       快递单号
     * addr_id            用户收货地址ID
     * @return \think\response\Json
     */
    public function sendOrder(){
        $order_id = input('id');
        $express_company = input('express_company');
        $express_code = input('express_code');
        $addr_id = input('addr_id');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDER = new OrderModel();
        $where['order_id'] = $order_id;
        $field = 'order_status,order_jiance_status';
        $order = $ORDER::findOrder($where, $field);
        if($order['order_status'] != 2) return json(['status' => 0, 'msg' => '订单状态异常']);

        $send = [
            'order_status' => 4,
            'order_user_express_company' => $express_company,
            'order_user_express_code' => $express_code,
            'order_user_send_time' => createTime()
        ];
        if($addr_id){
            $ADDRESS = new AddressModel();

            $whereAddress['addr_id'] = $addr_id;
            $fieldAddress = 'addr_username, addr_phone, concat(a1.district, a2.district, a3.district, addr_address) address';
            $addr = $ADDRESS::findJoinAddress($whereAddress, $fieldAddress);

            $send['order_user_addr_id'] = $addr_id;
            $send['order_user_name'] = $addr['addr_username'];
            $send['order_user_phone'] = $addr['addr_phone'];
            $send['order_user_address'] = $addr['address'];
        }

        $ORDER = new OrderModel();
        $res = $ORDER::updateOrder($where, $send);
        if($res){
            return json(['status' => 200, 'msg' => $this->doSuccess]);
        }else{
            return json(['status' => 0, 'msg' => $this->systemErr]);
        }
    }

    /**
     * 确认收货
     * id      订单ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function overOrder(){
        $order_id = input('id');

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDER = new OrderModel();
        $where['order_id'] = $order_id;
        $field = 'order_status';
        $order = $ORDER::findOrder($where, $field);
        if($order['order_status'] != 6) return json(['status' => 0, 'msg' => '订单状态异常']);

        $update = [
            'order_status' => 7,
            'order_user_shou_time' => createTime()
        ];

        $res = $ORDER::updateOrder($where, $update);
        if($res){
            return json(['status' => 200, 'msg' => $this->doSuccess]);
        }else{
            return json(['status' => 0, 'msg' => $this->systemErr]);
        }
    }

    /**
     * 物流信息
     * id      订单ID
     * type    1 寄往工厂   2 工厂回寄
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderWl(){
        $order_id = input('id');
        $type = input('type') ? input('type') : 1; // 1 寄往工厂   2 工厂回寄

        $result = (new IdMustPositiveInteger())->goCheck();
        if($result['status'] == 0) return json(['status' => 0, 'msg' => $result['msg']]);

        $ORDER = new OrderModel();
        $where['order_id'] = $order_id;
        $field = 'order_company_express_company, order_company_express_code,order_user_phone, order_user_express_company, order_user_express_code, order_company_phone';
        $field .= ', order_nj_user2, order_nj_user3';
        $wuLiu = $ORDER::findOrder($where, $field);
        $array = $this->arrayEmpty;
        $wuLiu['order_nj_user2'] = $wuLiu['order_nj_user2'] ? json_decode($wuLiu['order_nj_user2'],true) : $array;
        $wuLiu['order_nj_user3'] = $wuLiu['order_nj_user3'] ? json_decode($wuLiu['order_nj_user3'],true) : $array;
        if($type == 1){
            $express = [
                'code' => $wuLiu['order_user_express_code'],
                'phone' => $wuLiu['order_company_phone'],
                'company' => $wuLiu['order_user_express_company']
            ];
        }else{
            $express = [
                'code' => $wuLiu['order_company_express_code'],
                'phone' => $wuLiu['order_user_phone'],
                'company' => $wuLiu['order_company_express_company']
            ];
        }
        if($type == 3){
            $express = [
                'code' => $wuLiu['order_nj_user2']['order_company_express_code'],
                'phone' => $wuLiu['order_nj_user2']['order_user_phone'],
                'company' => $wuLiu['order_nj_user2']['order_company_express_company']
            ];
        }
        if($type == 4){
            $express = [
                'code' => $wuLiu['order_nj_user3']['order_company_express_code'],
                'phone' => $wuLiu['order_nj_user3']['order_user_phone'],
                'company' => $wuLiu['order_nj_user3']['order_company_express_company']
            ];
        }
        // 物流信息
        //*********** 获取快递公司编码
        $auto_url = 'http://www.kuaidi100.com/autonumber/auto?';
        $auto_data = [
            'num' => $express['code'],
            'key' => config('kd.zhnkey')
        ];
        $auto_data = http_build_query($auto_data);
        $auto_url_http = $auto_url . $auto_data;
        $request_auto = kd_http($auto_url_http); // 智能判断返回的数据
        if(isset($request_auto['returnCode'])){
            return json(['status' => 0, 'msg' => $request_auto['message']]);
        }else{
            //***************** 获取快递信息
            $search_url = 'http://poll.kuaidi100.com/poll/query.do';

            $post_data = [];
            $post_data["customer"] = config('kd.customer');
            $key = config('kd.key');
            if(!empty($request_auto)){
                $com = $request_auto[0]['comCode'];
                $post_data["param"] = '{"com":"' . $com . '","num":"' . $express['code'] . '"}';
                $post_data["sign"] = md5($post_data["param"] . $key . $post_data["customer"]);
                $post_data["sign"] = strtoupper($post_data["sign"]);
                $post_data = http_build_query($post_data);
                $search_data = kd_http($search_url, $post_data);
                // 返回失败信息
                if(isset($search_data['returnCode'])){
                    return json(['status' => 0, 'msg' => $search_data['message'], 'express' => $express]);
                }else{
                    return json(['status' => 200, 'msg' => $search_data['data'], 'express' => $express]);
                }
            }else{
                return json(['status' => 200, 'msg' => [], 'express' => $express]);
            }
        }
    }
}